﻿"use strict";
/// <reference path="../namespace.ts" />
/// <reference path="../repository/soc.ts" />
module WorkBasket.Design {

    declare var mvcModel: any;



    WorkbasketApp.controller('wbdsWorkflowCTRL',
        function ($scope) {
            var SOC = WorkBasket.SOC;

            $scope.FieldTypes = ['Text', 'Number', 'Date','Calculation'];
            $scope.Workflows = mvcModel.Workflows;
            $scope.SelectedFields = [];
            $scope.UnitID = mvcModel.UnitID;
            $scope.SelectedStep = null;
            $scope.ProcessVersions = [];
            $scope.WorkflowForms = [];
            $scope.SelectedProcessVersion = null;
            $scope.SelectedWorkflow = $scope.Workflows[0];
            $scope.OnWorkflowChange = function (workflow) {
                $scope.SelectedFields = SOC.Fields().GetByParentKey(workflow.WorkflowID);
                $scope.ProcessVersions = SOC.ProcessVersions().GetByParentKey(workflow.WorkflowID);
                $scope.WorkflowForms = SOC.Forms().GetByParentKey(workflow.WorkflowID);

                var pvid = $scope.ProcessVersions[0].ProcessVersionID;




                WorkBasket.WFTree.SetProcessVersionID(pvid,$scope);
                $("#jstree").jstree().refresh(false);
            }
            $scope.DeleteField = function (field) {
                var obj = SOC.Fields().Delete(field, field.FieldID);
                $scope.SelectedFields.splice($scope.SelectedFields.indexOf(field),1);
            }
            $scope.AddField = function () {
                var obj =  SOC.Fields().SaveData(new WorkBasket.Entities.Field(0, "My New Field",$scope.SelectedWorkflow.WorkflowID,"Text",""));
                $scope.SelectedFields.push(obj);
            }
            $scope.AddWorkflow = function () {
                var obj =  SOC.Workflows().SaveData(new WorkBasket.Entities.Workflow(0, "My New Workflow",$scope.UnitID));
                $scope.Workflows.push(obj);
                $scope.SelectedWorkflow = obj;
            }
            $scope.UpdateField = function (field) {
                SOC.Fields().SaveData(field);
            }
            $scope.UpdateWorkflow = function (Workflow) {
                SOC.Workflows().SaveData(Workflow);
            }
            $scope.DeleteWorkflow = function (Workflow) {
                var obj = SOC.Workflows().Delete(Workflow, Workflow.WorkflowID);
                $scope.Workflows.splice($scope.Workflows.indexOf(Workflow),1);
            }
            $scope.UpdateVersion = function (ProcessVersion) {
                var obj = SOC.ProcessVersions().SaveData(ProcessVersion);


            }
            $scope.SelectVersion = function (ProcessVersion) {
                $scope.SelectedProcessVersion = ProcessVersion;
                WorkBasket.WFTree.SetProcessVersionID(ProcessVersion.ProcessVersionID,$scope);
                $("#jstree").jstree().refresh(false);
            }
            $scope.DeleteVersion = function (ProcessVersion) {
                var obj = SOC.ProcessVersions().Delete(ProcessVersion, ProcessVersion.ProcessVersionID);
                $scope.ProcessVersions.splice($scope.ProcessVersions.indexOf(ProcessVersion), 1);


            }
            $scope.AddVersion = function () {
                var newversion = new WorkBasket.Entities.ProcessVersion(0, Date.now, "My New Version", $scope.SelectedWorkflow.WorkflowID);
                var obj = SOC.ProcessVersions().SaveData(newversion);
                $scope.ProcessVersions.push(obj);

            }
            $scope.AddRoot = function () {
                if ($scope.SelectedProcessVersion != null) {
                    WorkBasket.WFTree.addRoot();
                } else {
                    alert("Please Select a process version to edit");
                }
            }

            $scope.SaveStep= function () {
               
                SOC.Steps().SaveData($scope.SelectedStep);
             }

         })

}
